
import os
import subprocess


def add_water_mark(input_video):
    output_video_path = 'test_data/videos/run_water_mark.mp4'
    if os.path.exists(output_video_path):
        os.remove(output_video_path)
    try:
        print(f"使用 FFmpeg 添加水印到视频: {input_video} -> {output_video_path}")
        font_path = "font/simsun.TTF"  # 字体文件路径
        command = [
            'ffmpeg', '-y', '-i', input_video,
            '-vf', f'drawtext=text="此内容为AI生成":x=10:y=H-th-10:fontfile={
                font_path}:fontsize=20:fontcolor=white',
            '-c:v', 'libx264', '-c:a', 'aac', '-b:a', '192k', '-strict', 'experimental',
            output_video_path
        ]

        result = subprocess.run(
            command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
        print("FFmpeg 添加水印输出:", result.stdout)
        print("FFmpeg 添加水印错误:", result.stderr)
        if result.returncode != 0:
            print("FFmpeg 合并失败")
            return None
         # 检查输出视频是否成功生成
        if not os.path.exists(output_video_path):
            print(f"合并后的视频 {output_video_path} 未成功生成")
            return None
        print(f"处理完成，输出视频路径：{output_video_path}")
        return output_video_path

    except subprocess.CalledProcessError as e:
        print(f"FFmpeg 错误: {e}")
        print(f"错误输出: {e.stderr.decode() if e.stderr else '无详细错误信息'}")
        return None
    except Exception as e:
        print(f"发生了其他错误: {str(e)}")
        return None
